//
//  OosUser.m
//  Oos4ios
//
//  Created by Pablo Gonzalez on 3/21/11.
//  Copyright 2011 11870.com. All rights reserved.
//

#import <oos4ios/Model/OosUser.h>
#import <oos4ios/Model/OosMe.h>
#import <oos4ios/Model/OosService.h>
#import <oos4ios/Model/OosTag.h>
#import <oos4ios/Model/OosList.h>


@implementation OosUser

@dynamic nick;
@dynamic avatar;
@dynamic slug;
@dynamic webUrlString;
@dynamic userTags;
@dynamic userLists;

@dynamic services;
@dynamic reviews;
//@dynamic contacts;

@dynamic updatedActivities;
@dynamic updatedCheckins;
@dynamic updatedReviews;
@dynamic updatedServices;
@dynamic updatedContacts;
@dynamic tags;
@dynamic lists;


@dynamic contacts;

static NSString *fetchPredicate = @"slug = %@";

+ (OosUser *) userFromSlug:(NSString *)slug
{
    
    OosUser *result = [super objectWithPredicate: fetchPredicate, slug];
    
    if (!result) {
        result = [super create];
        result.slug = slug;
    }
	return result;
}

- (NSURL *)webUrl
{
    NSString *url = [self webUrlString];
    if (url) {
        return [NSURL URLWithString:url];
    } else {
        return nil;
    }
}

- (void)setWebUrl:(NSURL *)url
{
    if (!url) {
        self.webUrlString = nil;
    } else {
        self.webUrlString = url.absoluteString;
    }
}

- (BOOL)isLogged
{
    return [self isKindOfClass: [OosMe class]];
}

- (NSMutableSet *)mutableServices {
    return [self mutableSetValueForKeyPath:@"services"];
}

- (NSMutableSet *)mutableReviews {
    return [self mutableSetValueForKeyPath:@"reviews"];
}

- (NSMutableSet *)mutableUserTags {
    return [self mutableSetValueForKeyPath:@"userTags"];
}

- (NSMutableSet *)mutableUserLists {
    return [self mutableSetValueForKeyPath:@"userLists"];
}

- (NSMutableSet *) mutableTags
{
    return [self mutableSetValueForKey:@"tags"];
}

- (NSMutableSet *)mutableLists
{
    return [self mutableSetValueForKey:@"lists"];
}

- (NSFetchRequest *)requestForTag:(OosTag *)tag
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName: [OosService entityDescription].name];
    NSArray *predicates = [NSArray arrayWithObjects:
                           [NSPredicate predicateWithFormat:@"tags contains %@", tag.term], 
                           [NSPredicate predicateWithFormat:@"ANY users.slug = %@", self.slug], 
                           nil];
    NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:predicates];
    [request setPredicate:predicate];
    request.sortDescriptors = [NSArray arrayWithObjects:[NSSortDescriptor sortDescriptorWithKey:@"name" 
                                                                                      ascending:YES], nil];
    return request;
}

- (NSArray *)servicesForTag:(OosTag *)tag
{
    NSError *error = nil;
    NSArray *result = nil;
    if ((result = [[self managedObjectContext] executeFetchRequest: [self requestForTag:tag] 
                                                            error: &error]) == nil) {
        NSLog(@"Error fetching services for tag(%@): %@", tag, [error localizedDescription]);
    }
    return result;
}

- (NSUInteger)countServicesForTag:(OosTag *)tag 
{
    NSError *error = nil;
    NSUInteger result;
    if (result = [[self managedObjectContext] countForFetchRequest: [self requestForTag:tag] 
                                                             error: &error]) {
        return result;
    }
    NSLog(@"Error counting services for tag: %@", [error localizedDescription]);
    return 0;
}

- (NSFetchRequest *)requestForList:(OosList *)list
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName: [OosService entityDescription].name];
    NSArray *predicates = [NSArray arrayWithObjects:
                           [NSPredicate predicateWithFormat:@"lists contains %@", list.term], 
                           [NSPredicate predicateWithFormat:@"ANY users.slug = %@", self.slug], nil];
    NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:predicates];
    [request setPredicate:predicate];
    request.sortDescriptors = [NSArray arrayWithObjects:[NSSortDescriptor sortDescriptorWithKey:@"name" 
                                                                                      ascending:YES], nil];
    return request;
}

- (NSArray *)servicesForList:(OosList *)list
{
    NSError *error = nil;
    NSArray *result = nil;
    if ((result = [[self managedObjectContext] executeFetchRequest: [self requestForList:list] 
                                                             error: &error]) == nil) {
        NSLog(@"Error fetching services for lisy(%@): %@", list, [error localizedDescription]);
    }
    return result;
}

- (NSUInteger)countServicesForList:(OosList *)list 
{
    NSError *error = nil;
    NSUInteger result;
    if (result = [[self managedObjectContext] countForFetchRequest: [self requestForList:list] 
                                                             error: &error]) {
        return result;
    }
    NSLog(@"Error: %@", [error localizedDescription]);
    return 0;
}

@end
